<template>
  <div class="cart">
    <h4>全部商品</h4>
    <div class="cart-main">
      <div class="cart-th">
        <div class="cart-th1">全部</div>
        <div class="cart-th2">商品</div>
        <div class="cart-th3">单价（元）</div>
        <div class="cart-th4">数量</div>
        <div class="cart-th5">小计（元）</div>
        <div class="cart-th6">操作</div>
      </div>
      <div class="cart-body">
        <ul class="cart-list" v-for="cartInfo in cartInfoList" :key="cartInfo.id">
          <li class="cart-list-con1">
            <input type="checkbox" name="chk_list" :checked='!!cartInfo.isChecked' @change="checkedinfostate(cartInfo)">
          </li>
          <li class="cart-list-con2">
            <img :src="cartInfo.imgUrl">
            <div class="item-msg">{{cartInfo.skuName}}</div>
          </li>
          <li class="cart-list-con4">
            <span class="price">{{cartInfo.skuPrice}}</span>
          </li>
          <li class="cart-list-con5">
            <a href="javascript:void(0)" class="mins" @click="commodity_quantity(cartInfo.skuId,cartInfo)">-</a>

            <input autocomplete="off" type="text" :value="cartInfo.skuNum" minnum="1" class="itxt"  @change="commodity_quantity_input(cartInfo.skuId,cartInfo,$event)">

            <a href="javascript:void(0)" class="plus" @click="commodity_quantity_plus(cartInfo.skuId,cartInfo)" >+</a>
          </li>
          <li class="cart-list-con6">
            <span class="sum">{{cartInfo.skuPrice*(cartInfo.skuNum)}}</span>
          </li>
          <li class="cart-list-con7">
            <a href="#none" class="sindelet" @click="isshowa('yitiao',cartInfo)">删除</a>

            <br>
            <a href="#none">移到收藏</a>
          </li>
          <!-- //弹出提示框 -->
          <Dialog v-show="isshow" :cancel="cancel">
            <template v-slot:header>
              <h3>提示</h3>
            </template>
            <template>
              <p>真的要删除{{tishi}}数据吗？</p>
            </template>

            <template #footer>
              <div class="btn-footer">
                <button class="btn" @click="isshow=false">取消</button>
                <button class="btn btn-primary" @click="dele">确认</button>
              </div>
            </template>
          </Dialog>
        </ul>

      </div>
    </div>
    <div class="cart-tool">
      <div class="select-all">
        <input class="chooseAll" type="checkbox" v-model="checked">
        <span>全选</span>
      </div>
      <div class="option">
        <a href="#none" @click="isshowa('piliang')">删除选中的商品</a>
        <a href="#none">移到我的关注</a>
        <a href="#none">清除下柜商品</a>
      </div>
      <div class="money-box">
        <div class="chosed">已选择
          <span>{{howManya}}</span>件商品
        </div>
        <div class="sumprice">
          <em>总价（不含运费） ：</em>
 
          <i class="summoney">{{grossAmount}}</i>
        </div>
        <div class="sumbtn">
          <router-link class="sum-btn"  to="/Trade">结算</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { reqShopCartListData, reqcheckCartliststate, reqChangeAllCartState, reqdeldeleteCart ,reqdeldeleteCartbatch ,reqaddToCartlistd} from '@/api/index'
export default {
  name: 'ShopCart',
  data() {
    return {
      cartInfoList: [],
      isshow: false,
      tishi: '',
      skuId: '',
      skuIdList: [],//储存批量删除被选择的商品时的 skuid
  
    }
  },
  methods: {
    // 获取购物车数据
    async shuju() {
      const result = await reqShopCartListData()
      if (result.code === 200) {
        console.log('购物车数据请求成功')
      } else {
        console.log('购物车数据请求失败')
      }
      console.log(result)
      if (result.data.length) {
        this.cartInfoList = result.data[0].cartInfoList
        // console.log(result.data[0].cartInfoList)
      }
    },

    async checkedinfostate(cartInfo) {
      let isChecked = 1 - cartInfo.isChecked
      const result = await reqcheckCartliststate(cartInfo.skuId, isChecked)
      //更新服务器数据成功后，重新更新页面
      cartInfo.isChecked = isChecked
    },
    cancel(val) {
      this.isshow = val
    },

    // 删除一条或批量提示
    isshowa(val, cartInfo) {
      if (val === 'yitiao') {
        this.tishi = '一条'
      this.skuId = cartInfo.skuId

      } else {
        this.tishi = '贼多条'
     skuIdLista=  this.cartInfoList.filter(item => {
          return item.isChecked 
          
     })

        
    skuIdLista.forEach(item => {
        return this.skuIdList.push(item.skuId)
      })
       
        // this.skuIdList.push()
      }
      this.isshow = true
    },

    //删除一条购物车中的数据或批量删除
    async dele() {
      if (this.tishi === '一条') {
        const result = await reqdeldeleteCart(this.skuId)
        console.log('单挑')
        if (result.code === 200) this.shuju()
        this.isshow = false
      } else {
        console.log(this.skuIdList);
        this.isshow = false
        const resulta = await reqdeldeleteCartbatch(this.skuIdList)
        if (resulta.code === 200) 
          console.log('批量删除成功');

            this.shuju()
        
      }
    },

    // 更改购物车商品数量减
    async commodity_quantity(skuId,cartInfo) {
      let skuNumx = 0
       skuNumx--
        if(cartInfo.skuNum<2)return
        const result = await reqaddToCartlistd(skuId, skuNumx)
        if (result.code === 200)
        this.shuju()
    },

    // 更改购物车商品数量加
    async commodity_quantity_plus(skuId,cartInfo) {
      let skuNumx = 0
       skuNumx++
        if(cartInfo.skuNum>199)return
        const result = await reqaddToCartlistd(skuId, skuNumx)
        if (result.code === 200)
        this.shuju()
    },


    //通过input框直接修改商品数量
    async commodity_quantity_input(skuId, cartInfo, e) {
      let Numx = e.target.value //获取用户输入的值
  
      //  当前输入的值减去原来的值等于要修改的值
      let sNumx = Numx - cartInfo.skuNum
 
     
      cartInfo.skuNum = Numx
            if ( cartInfo.skuNum > 200) {
              cartInfo.skuNum = 200
               sNumx=0
      } else if ( cartInfo.skuNum < 1) {
              cartInfo.skuNum = 1
                sNumx=0
      }
      const result = await reqaddToCartlistd(skuId, sNumx)
    
        this.shuju()
       
     
   
  
    },
  },

  // 计算属性
  computed: {
    checked: {
      get() {
        return this.cartInfoList.every((cartInfo) => {
          return cartInfo.isChecked === 1
        })
      },
      async set(val) {
        let isChecked = val ? 1 : 0
        const skuIdlist = []

        this.cartInfoList.forEach((cartInfo) => {
          cartInfo.isChecked = isChecked
          skuIdlist.push(cartInfo.skuId)
        })
        //统一批量更新选中状态
        const result = await reqChangeAllCartState(isChecked, skuIdlist)
        if (result.code === 200) {
          alert('更改成功')
        } else {
          alert('更改失败')
        }
      },

    },
    
      //计算所选商品总数
      howManya(){
        return this.cartInfoList.filter(item => (item.isChecked )).length
    },
          //计算总金额
    grossAmount() {
     return this.cartInfoList.reduce(
        (prev, current) =>
          prev + current.isChecked * current.skuPrice * current.skuNum,
        0
      );
           
          }
  },
  //监视属性
  watch: {
    isshow: {
      immediate: true,
      handler() {
        if (this.isshow) {
          document.documentElement.style.overflow = 'hidden'
        } else {
          document.documentElement.style.overflow = 'auto'
        }
      }
    }
  },
  //获取服务器购物车数据
   mounted() {
    this.shuju()
  }
}
</script>

<style lang="less" scoped>
.cart {
  width: 1200px;
  margin: 0 auto;
  h4 {
    margin: 9px 0;
    font-size: 14px;
    line-height: 21px;
  }
  .cart-main {
    .cart-th {
      background: #f5f5f5;
      border: 1px solid #ddd;
      padding: 10px;
      overflow: hidden;
      & > div {
        float: left;
      }
      .cart-th1 {
        width: 25%;
        input {
          vertical-align: middle;
        }
        span {
          vertical-align: middle;
        }
      }
      .cart-th2 {
        width: 25%;
      }
      .cart-th3,
      .cart-th4,
      .cart-th5,
      .cart-th6 {
        width: 12.5%;
      }
    }
    .cart-body {
      margin: 15px 0;
      border: 1px solid #ddd;
      .cart-list {
        padding: 10px;
        border-bottom: 1px solid #ddd;
        overflow: hidden;
        & > li {
          float: left;
        }
        .cart-list-con1 {
          width: 15%;
        }
        .cart-list-con2 {
          width: 35%;
          img {
            width: 82px;
            height: 82px;
            float: left;
          }
          .item-msg {
            float: left;
            width: 150px;
            margin: 0 10px;
            line-height: 18px;
          }
        }
        .cart-list-con4 {
          width: 10%;
        }
        .cart-list-con5 {
          width: 17%;
          .mins {
            border: 1px solid #ddd;
            border-right: 0;
            float: left;
            color: #666;
            width: 6px;
            text-align: center;
            padding: 8px;
          }
          input {
            border: 1px solid #ddd;
            width: 40px;
            height: 33px;
            float: left;
            text-align: center;
            font-size: 14px;
          }

          .plus {
            border: 1px solid #ddd;
            border-left: 0;
            float: left;
            color: #666;
            width: 6px;
            text-align: center;
            padding: 8px;
          }
        }

        .cart-list-con6 {
          width: 10%;

          .sum {
            font-size: 16px;
          }
        }

        .cart-list-con7 {
          width: 13%;

          a {
            color: #666;
          }
        }
      }
    }
  }
  .cart-tool {
    overflow: hidden;
    border: 1px solid #ddd;

    .select-all {
      padding: 10px;
      overflow: hidden;
      float: left;

      span {
        vertical-align: middle;
      }

      input {
        vertical-align: middle;
      }
    }

    .option {
      padding: 10px;
      overflow: hidden;
      float: left;

      a {
        float: left;
        padding: 0 10px;
        color: #666;
      }
    }

    .money-box {
      float: right;

      .chosed {
        line-height: 26px;
        float: left;
        padding: 0 10px;
      }

      .sumprice {
        width: 200px;
        line-height: 22px;
        float: left;
        padding: 0 10px;

        .summoney {
          color: #c81623;
          font-size: 16px;
        }
      }

      .sumbtn {
        float: right;

        a {
          display: block;
          position: relative;
          width: 96px;
          height: 52px;
          line-height: 52px;
          color: #fff;
          text-align: center;
          font-size: 18px;
          font-family: 'Microsoft YaHei';
          background: #e1251b;
          overflow: hidden;
        }
      }
    }
  }
}

.btn-footer {
  position: absolute;
  right: 40px;
  bottom: 20px;
  .btn {
    width: 62px;
    height: 32px;
    font-size: 14px;
    margin-left: 16px;
    border-width: 1px;
    border-radius: 10%;
    cursor: pointer;
    &:hover {
      background-color: rgb(0, 255, 128);
      color: rgb(232, 11, 11);
    }
    &.btn-primary {
      background-color: rgb(102, 177, 255);
      color: #fff;
      &:hover {
        background-color: rgb(115, 255, 0);
        color: rgb(11, 188, 232);
      }
    }
  }
}
</style>